//
//  MK_BarrageShowCell.m
//  miku直播
//
//  Created by 杨尚达 on 2017/7/29.
//  Copyright © 2017年 杨尚达. All rights reserved.
//

#import "MK_BarrageShowCell.h"
#import "MK_BarrageManager.h"
#import "MK_BarrageShowCell_Cell.h"
#import "MK_BarrageModel+ProtocolAdd.h"

#define MK_BarrageShowCell_CellName @"MK_BarrageShowCell_CellName"

@interface MK_BarrageShowCell () <UITableViewDelegate,UITableViewDataSource>

///弹幕模型数组
@property(nonatomic,strong)NSMutableArray<id<MK_Barrage_Protocol>>* barrageArr;

///弹幕tableView
@property(atomic,weak)UITableView* barrageTBV;

///头部视图
@property(nonatomic,weak)UILabel* headLa;

@end

@implementation MK_BarrageShowCell

- (void)awakeFromNib {
    [super awakeFromNib];
    [self setupUI];
    [self addNotification];
}

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        [self addNotification];
    }
    return self;
}



///初始化界面
-(void)setupUI{
    self.barrageArr = [NSMutableArray new];
    UITableView* barrageTBV = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
    _barrageTBV = barrageTBV;
    [barrageTBV registerNib:[UINib nibWithNibName:@"MK_BarrageShowCell_Cell" bundle:nil] forCellReuseIdentifier:MK_BarrageShowCell_CellName];
    barrageTBV.backgroundColor = [UIColor groupTableViewBackgroundColor];
    barrageTBV.delegate = self;
    barrageTBV.dataSource = self;
    barrageTBV.rowHeight = 30;
    barrageTBV.separatorStyle = UITableViewCellSeparatorStyleNone;
    barrageTBV.tableFooterView = [UIView new];
    [self addSubview:barrageTBV];
    [barrageTBV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.offset(0);
    }];
    UILabel* headLa = [UILabel new];
    _headLa = headLa;
    headLa.numberOfLines = 0;
    headLa.textColor = [UIColor orangeColor];
    headLa.font = [UIFont systemFontOfSize:13];
    headLa.backgroundColor = [UIColor groupTableViewBackgroundColor];
    headLa.frame = CGRectMake(0, 0, 375, 50);
    UIView* headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 375, 50)];
    headView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [headView addSubview:headLa];
    barrageTBV.tableHeaderView = headView;
    [headLa mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.left.offset(10);
        make.right.offset(-10);
    }];
}

///添加通知
-(void)addNotification{
    ///普通弹幕通知
    [[[NSNotificationCenter defaultCenter]rac_addObserverForName:NotificationOfMK_BarrageModelSend object:nil] subscribeNext:^(NSNotification*  _Nullable x) {
        [self addBarrageToViewWithBarrage:x.object];    }];
    [[[NSNotificationCenter defaultCenter]rac_addObserverForName:NotificationOfMK_UseLogInRoom object:nil] subscribeNext:^(NSNotification*  _Nullable x) {
        [self addBarrageToViewWithBarrage:x.object];
    }];
}

#pragma mark 添加弹幕的方法
///添加弹幕的方法
-(void)addBarrageToViewWithBarrage:(id<MK_Barrage_Protocol>)barrage{
    [[NSOperationQueue mainQueue] addOperationWithBlock:^{
        [self.barrageArr addObject:barrage];
        [_barrageTBV reloadData];
        [_barrageTBV scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:self.barrageArr.count-1 inSection:0] atScrollPosition:UITableViewScrollPositionNone animated:YES];
    }];
}


#pragma mark 代理方法
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.barrageArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MK_BarrageShowCell_Cell* cell = [tableView dequeueReusableCellWithIdentifier:MK_BarrageShowCell_CellName forIndexPath:indexPath];
    cell.model = self.barrageArr[indexPath.row];
    return cell;
}
-(void)setModel:(MK_LivingRoomModel *)model{
    _model = model;
    _headLa.text = [NSString stringWithFormat:@"欢迎来到 %@ 直播间,miku直播提倡健康的直播环境,对直播内容进行24小时巡查,任何传播违法,违规,低俗等不良信息将被封号",model.roomName];
}



@end
